# Memento proxy for National Taiwan University

import sys, os, re
import urllib
from baseHandler import *
import urllib2
__author__="aalsum"
__date__ ="$Mar 7, 2013 10:14:36 AM$"

baseuri = "http://webarchive.lib.ntu.edu.tw/eng/advlist.asp"
basehostname = "http://webarchive.lib.ntu.edu.tw/"

class TaiwanHandler(BaseProxyHandler):

    def fetch_changes(self, req, requri, dt=None):
        # implement the changes list for this particular proxy

        param = {}
        param['event']='000'
        param['field1']='webaddr'
        param['field2']='sitenam'
        param['field3']='sitenam'
        param['field4']='sitenam'
        param['field5']='sitenam'
        param['field6']='sitenam'
        param['logic1']=1
        param['logic2']=1
        param['logic3']=1
        param['logic4']=1
        param['logic5']=1
        param['project']=000
        param['strKeyWord1']=requri
        param['strKeyWord2']=''
        param['strKeyWord3']=''
        param['strKeyWord4']=''
        param['strKeyWord5']=''
        param['strKeyWord6']=''
        param['webFirstClass']='000'
        param['webSecondClass']='000'
        param['webThirdClass']='000'


        request = urllib2.Request(baseuri, urllib.urlencode(param))
        opener = urllib2.build_opener()


       
        try:
            response = opener.open(request)
            data = response.read()
        except:
            return self.error("Couldn't retrieve data from %s" % baseuri, req, status=404)

        opener.close();

        changes = []

        tmRegex = re.compile('<a class="herf1" title=".*" target="\_self" href=".*">');
        result = tmRegex.search( data)
        if result:
             tmuri = result.group(0)
             d=tmuri.find("href=")
             tmuri = tmuri[d+6:-2]
             tmuri = basehostname + tmuri

        else:
            return changes;


        try:
            fh = urllib.urlopen(tmuri)
        except:
            return self.error("Couldn't retrieve data from %s" % baseuri, req, status=404)

        data = fh.read()
        fh.close()


        uriRegex =re.compile( r'<a target="_self" href=".*" style="text-decoration:underline;color:slategray" onmouseover="this.style.color=\'red\'" onmouseout="this.style.color=\'slategray\'">');

        


        uris = re.findall(uriRegex, data)
        for u in uris:
            d = u.find("style")
            loc = basehostname+u[24:d-2]

            dtstr= loc[-10:]
            dtstr = dtstr.replace("-","")+"000000 GMT"
            
            dtobj = dateparser.parse(dtstr)

            changes.append((dtobj, loc, {'last': dtobj, 'obs' : 1}))

        return changes


def handler(req):
    hdlr = TaiwanHandler('tw')
    return basehandler(req, hdlr)


